#!/bin/bash

adduser_remote(){
  if [ "x$1" = "x" ]; then
    die "missing host"
  fi
  ssh -o StrictHostKeyChecking=no $1 "egrep \"^${HYREN_USER}:\" /etc/group > /dev/null || { groupadd $HYREN_USER; }"
  ssh -o StrictHostKeyChecking=no $1 "egrep \"^${HYREN_USER}:\" /etc/passwd > /dev/null || { useradd -s /bin/bash  -g $HYREN_USER $HYREN_USER; }"
  ssh -o StrictHostKeyChecking=no $1 "egrep \"^${HYREN_USER}:\" /etc/passwd > /dev/null" || { die "Add user [$HYREN_USER] fail on $1"; }
  ssh -o StrictHostKeyChecking=no $1 "echo $HYREN_USER:$HYREN_USERPWD | chpasswd" || { die "set user [$HYREN_USER] password to [$HYREN_USERPWD] fail on $1"; }
  ssh -o StrictHostKeyChecking=no $1 "if [ ! -d $HYRENTMP_HOME ]; then mkdir -p $HYRENTMP_HOME; fi" || { die "create $HYRENTMP_HOME fail on $1"; }
  ssh -o StrictHostKeyChecking=no $1 "chown -R $HYREN_USER:$HYREN_USER $HYRENTMP_HOME" || { die "chown to $HYRENTMP_HOME fail on $1"; }
}
# 在远程主机上创建海云用户
init_oper()
{
  echo_start "$1  ... ..."
  confirm_operate "create hyren user on remote host"
  if [ $? -eq 0 ]; then
    # 远程创建海云用户
    local value_arr=($HYREN_ALLHOSTS)
    for i in ${value_arr[@]}; do
      # 如果本机在ALLHOSTS中，则跳过
      if [ "${i}" == "$HYRENSERV_HOSTNAME" ]; then echo_warn "skip ${i}"; continue; fi
      echo_info "create hyren user on [${i}]"
      adduser_remote "$i"
    done
    echo_success "Create hyren user on remote host done."

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}